/* @(#)GSLoginAction.java    
*
* Project: BUS
*
* Modify Information:
* =============================================================================
*   Author       Date       Description
*   ------------ ---------- ---------------------------------------------------
*   Simonyi Peng 2006-2-7    建立
*
* Copyright Notice:
* =============================================================================
*    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
*        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
*        Haidian District, Beijing ,100081, China All rights reserved.
*
*    This software is the confidential and proprietary information of
*        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
*        You shall not disclose such Confidential Information and shall use it
*        only in accordance with the terms of the license agreement you entered
*        into with HiTRUST.
*
* Warning:
* =============================================================================
* 
*/
package com.hitrust.gs.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;

import system.LoginContext;

import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.business.GSBusOrderProcesser;
import com.hitrust.gs.business.GSBusOrgProcessor;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.form.GSOrderAuthorizeSaveForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSOrderAuthorizeSaveVO;

/**
 * @author simonyi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class GSOrderAuthorizeSaveAction extends GSBaseAction {
	public String execute(GSBaseForm aForm) throws Exception {
	    
	    LoginContext operatorBean = (LoginContext)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        
        GSOrderAuthorizeSaveForm tForm = (GSOrderAuthorizeSaveForm)aForm;
        String[] detailCheckbox = tForm.getIndexs();
        String[] detailId = tForm.getDetailId();
        String[] orderCount = tForm.getOrderCount();
        String[] oldOrderCount = tForm.getOldOrderCount();
        String[] providerId = tForm.getNewProviderId();
        String[] oldProviderId = tForm.getOldProviderId();
        String[] unitPrice = tForm.getUnitPrice();
        String[] oldUnitPrice = tForm.getOldUnitPrice();
        String[] manufactureId = tForm.getManufactureId();
        String[] oldManufactureId = tForm.getOldManufactureId();
        String[] orderId = tForm.getOrderId();
        String[] oldmode=tForm.getOldmode();
        String[] mode=tForm.getMode();
        String msg = tForm.getAuditcontent();
        
        // add by Jackey start
        //增加对送货日期和送货时间的设置
        String[] deliverDate = tForm.getDeliverDate();
        String[] deliverTime = tForm.getDeliverTime();
        // add by Jackey end

        String confirmorreject = aForm.getRequest().getParameter("confirmorreject");
        
        ArrayList list = new ArrayList();
        for(int i=0;i<detailCheckbox.length;i++){
            for(int j=0;j<detailId.length;j++){
                if(detailId[j].equals(detailCheckbox[i])){
                    GSOrderAuthorizeSaveVO vo = new GSOrderAuthorizeSaveVO ();
                    vo.detailId = detailId[j];
                    if("T".equals(confirmorreject)) {//如果为审核不通过，不用取得下列信息
                        vo.manufactureId = manufactureId[j];
                        vo.orderCount = orderCount[j];
                        vo.unitPrice = unitPrice[j];
                        vo.providerId = providerId[j];
                        vo.orderId = orderId[j];
                        vo.mode=mode[j];
                        //记录原始值
                        vo.oldManufactureId = oldManufactureId[j];
                        vo.oldOrderCount = oldOrderCount[j];
                        vo.oldProviderId = oldProviderId[j];
                        vo.oldUnitPrice = oldProviderId[j];
                        vo.oldmode=oldmode[j];
    					//记录送货日期和送货时间
    					vo.deliverDate = deliverDate[j];
    					vo.deliverTime = deliverTime[j];
                    }
                    list.add(vo);
                }
            }
        }

        
        GSBusOrderProcesser oproc = new GSBusOrderProcesser();
        try{
            if("T".equals(confirmorreject)) {
                oproc.authorizeOrder(operatorBean,list);
            }
            if("F".equals(confirmorreject)) {
                String[] indexs = {((GSOrderAuthorizeSaveVO)list.get(0)).detailId};
            	oproc.unAuthorizeOrder(indexs,operatorBean,msg);
            }
            
	        //返回到qryaction时，给出是否重新查询的指示
	        aForm.getRequest().setAttribute("ISQUERYCONDITIONEXIST","T");
	        if("T".equals(confirmorreject)) {
	            aForm.getRequest().setAttribute("SUCCESSMSG","订单审核通过执行成功");
            }
            if("F".equals(confirmorreject)) {
                aForm.getRequest().setAttribute("SUCCESSMSG","订单审核不通过执行成功");
            }
	        
	        
            GSBusOrgProcessor proc = new GSBusOrgProcessor ();
            //查询产品等级
            GSManufactureProcessor mproc = new GSManufactureProcessor();
            mproc.getManufactureOfLevel(aForm);
            
            //查询机构-送货地址
            GSUniteGwProcessor processer = new GSUniteGwProcessor();
            ArrayList ary = processer.getWareHouseLevelName(operatorBean.getLoginWarehouseCode(),operatorBean.getLoginWarehouseCode());
            aForm.getRequest().setAttribute("organdaddr",ary);

        }catch(GSException e){
            aForm.getRequest().setAttribute("action", "A150201.do");
            throw e;
        }
        
        return GSBaseConstants.FORWARD_SUCCESS;
        
	}
}
